package com.hyd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hyd.common.annotation.Log;
import com.hyd.common.core.controller.BaseController;
import com.hyd.common.core.domain.AjaxResult;
import com.hyd.common.enums.BusinessType;
import com.hyd.manage.domain.Medicines;
import com.hyd.manage.service.IMedicinesService;
import com.hyd.common.utils.poi.ExcelUtil;
    import com.hyd.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 药品入库Controller
 *
 * @author hwb
 * @date 2025-02-20
 */
@Api(tags = "药品入库Controller")
@RestController
@RequestMapping("/manage/medicines")
public class MedicinesController extends BaseController
{
    @Autowired
    private IMedicinesService medicinesService;

    /**
     * 查询库存列表
     */
    @ApiOperation("查询库存列表")
    @PreAuthorize("@ss.hasPermi('manage:medicines:list')")
    @GetMapping("/stock")
    public TableDataInfo stock(Medicines medicines)
    {
        //startPage();
        List<Medicines> list = medicinesService.selectMedicinesList(medicines);
        // 将其中药品编码一样的数据合并
        for (int i = 0; i < list.size(); i++) {
            Medicines medicines1 = list.get(i);
            for (int j = i + 1; j < list.size(); j++) {
                Medicines medicines2 = list.get(j);
                if (medicines1.getMedicineCode().equals(medicines2.getMedicineCode())) {
                    medicines1.setStockQuantity(medicines1.getStockQuantity() + medicines2.getStockQuantity());
                    //从list集合中去除掉重复的数据
                    list.remove(j);
                }
            }
            // 更新list集合中重复的数据
            list.set(i, medicines1);
        }
        startPage();
        return getDataTable(list);
    }

    /**
     * 查询药品入库列表
     */
    @ApiOperation("查询药品入库列表")
    @PreAuthorize("@ss.hasPermi('manage:medicines:list')")
    @GetMapping("/list")
    public TableDataInfo list(Medicines medicines)
    {
        startPage();
        List<Medicines> list = medicinesService.selectMedicinesList(medicines);
        return getDataTable(list);
    }

    /**
     * 导出药品入库列表
     */
    @ApiOperation("导出药品入库列表")
    @PreAuthorize("@ss.hasPermi('manage:medicines:export')")
    @Log(title = "药品入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Medicines medicines)
    {
        List<Medicines> list = medicinesService.selectMedicinesList(medicines);
        ExcelUtil<Medicines> util = new ExcelUtil<Medicines>(Medicines.class);
        util.exportExcel(response, list, "药品入库数据");
    }

    /**
     * 获取药品入库详细信息
     */
    @ApiOperation("获取药品入库详细信息")
    @PreAuthorize("@ss.hasPermi('manage:medicines:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(medicinesService.selectMedicinesById(id));
    }

    /**
     * 新增药品入库
     */
    @ApiOperation("新增药品入库")
    @PreAuthorize("@ss.hasPermi('manage:medicines:add')")
    @Log(title = "药品入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Medicines medicines)
    {
        return toAjax(medicinesService.insertMedicines(medicines));
    }

    /**
     * 修改药品入库
     */
    @ApiOperation("修改药品入库")
    @PreAuthorize("@ss.hasPermi('manage:medicines:edit')")
    @Log(title = "药品入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Medicines medicines)
    {
        return toAjax(medicinesService.updateMedicines(medicines));
    }

    /**
     * 删除药品入库
     */
    @ApiOperation("删除药品入库")
    @PreAuthorize("@ss.hasPermi('manage:medicines:remove')")
    @Log(title = "药品入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(medicinesService.deleteMedicinesByIds(ids));
    }

}